package cn.fenqing168.nio;

import lombok.SneakyThrows;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author fenqing
 */
public class NioClient1 {

    @SneakyThrows
    public static void main(String[] args) {

        //打开一个socketChannel
        SocketChannel socketChannel = SocketChannel.open();
        //新建一个端口
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 9999);
        //连接
        if(!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.isConnected()){
                System.out.println("没有连接好");
            }
        }

        String str = "测试发送消息";
        System.out.println("开始发送");
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8));
        socketChannel.write(buffer);
        System.out.println("发送完毕");



    }

}
